package com.egghiro.framework.j2se.game.core.key;

import java.awt.event.KeyEvent;
import java.util.HashMap;

/**
 * TODO 尚未实现改键
 * @project SGame
 * @date: 2013-8-18
 * @version 0.1
 */
public class SKeyStateJ2SEInput {
//    private static byte[] lock = new byte[0]; //在SKeyState里有Lock，此处不需要
    private SKeyState sKeyState;
    
    private static HashMap<String, SKeyStateJ2SEInput> sKeyStateJ2SEInputMap = new HashMap<String, SKeyStateJ2SEInput>();
    
    public static SKeyStateJ2SEInput getSKeyStateJ2SEInput(String playerId){
        if (sKeyStateJ2SEInputMap == null) {
            synchronized (SKeyStateJ2SEInput.class) {
                if (sKeyStateJ2SEInputMap == null) {
                    sKeyStateJ2SEInputMap = new HashMap<String, SKeyStateJ2SEInput>();
                }
            }
        }
        SKeyStateJ2SEInput sKeyStateJ2SEInput = sKeyStateJ2SEInputMap.get(playerId);
        if (sKeyStateJ2SEInput == null) {
            synchronized (sKeyStateJ2SEInputMap) {
                if (sKeyStateJ2SEInputMap.get(playerId) == null) {
                    sKeyStateJ2SEInput = new SKeyStateJ2SEInput(SKeyState.getSKeyState(playerId));
                    sKeyStateJ2SEInputMap.put(playerId, sKeyStateJ2SEInput);
                }
            }
        }
        return sKeyStateJ2SEInput;
    }
    
    public static void removePlayer(String playerId){
        if (sKeyStateJ2SEInputMap != null) {
            sKeyStateJ2SEInputMap.remove(playerId);
        }
    }
    
    public static void removeAllPlayers(){
        if (sKeyStateJ2SEInputMap != null) {
            sKeyStateJ2SEInputMap.clear();
        }
    }
    
    public SKeyStateJ2SEInput(SKeyState sKeyState){
        this.sKeyState = sKeyState;
    }
    
    public void keyPressed(KeyEvent e) {
        // TODO 尚未实现改键
//        synchronized (lock) {
            switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                this.sKeyState.pressKeyUp();
                break;
            case KeyEvent.VK_DOWN:
                this.sKeyState.pressKeyDown();
                break;
            case KeyEvent.VK_LEFT:
                this.sKeyState.pressKeyLeft();
                break;
            case KeyEvent.VK_RIGHT:
                this.sKeyState.pressKeyRight();
                break;
            default:
                break;
            }
//        }
    }

    public void keyReleased(KeyEvent e) {
        // TODO 尚未实现改键
//        synchronized (lock) {
            switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                this.sKeyState.releaseKeyUp();
                break;
            case KeyEvent.VK_DOWN:
                this.sKeyState.releaseKeyDown();
                break;
            case KeyEvent.VK_LEFT:
                this.sKeyState.releaseKeyLeft();
                break;
            case KeyEvent.VK_RIGHT:
                this.sKeyState.releaseKeyRight();
                break;
            default:
                break;
            }
//        }
    }
}
